//
//  BusinessView.m
//  小团购
//
//  Created by 樊荣海 on 15/2/9.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "BusinessView.h"
#define maxRow 3    //最多显示3个business
#define rowH 60

@implementation BusinessView

+ (id)businessView {

    return [[NSBundle mainBundle] loadNibNamed:@"BusinessView" owner:nil options:nil][0];
}

- (void)awakeFromNib {

    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
}

- (void)setDealModel:(DealModel *)dealModel {
    
    _dealModel = dealModel;
    int count = _dealModel.businesses.count<maxRow ? _dealModel.businesses.count:maxRow;
    
    //与之前相差多少
    CGFloat newHeight = rowH * count;
    
    CGFloat daltaH = newHeight-_tableView.frame.size.height;
    
    //改变 tableView
    CGRect rect = _tableView.frame;
    rect.size.height = newHeight;
    _tableView.frame = rect;
    
    //改变 整体
    CGRect rect2 = self.frame;
    rect2.size.height += daltaH+2;
    self.frame = rect2;

//    NSLog(@"sb atbleView :%@", _tableView);

    [_tableView reloadData];

    //total
    [_totalBtn setTitle:[NSString stringWithFormat:@"共有%d个商家", _dealModel.businesses.count] forState: UIControlStateNormal];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dealModel.businesses.count<maxRow ? _dealModel.businesses.count:maxRow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *ID = @"BusinessCell";
    
    BusinessCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"BusinessCell" owner:nil options:nil][0];
    }
    
    
    
    BusinessModel *model = _dealModel.businesses[indexPath.row];

    cell.upTitle = model.name;
    cell.downTitle = model.address;
    cell.indexPath = indexPath;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [MBTool showHUDWithText:@"此功能未完成" inView:self Delay:1.5];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 60;
}

@end
